package com.tripshare.logic.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.google.common.collect.ImmutableMap;
import com.tripshare.logic.interceptor.AccessInterceptor;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
@EnableWebMvc
public class WebApplicationConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/**");
    }


    //使用这个方法会覆盖默认的HttpMessageConverter,本次工程大约有7个spring HttpMessageConverter
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//
//        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        converters.add(mappingJackson2HttpMessageConverter);
//        super.configureMessageConverters(converters);
//    }


    @Bean
    public ServletRegistrationBean servletRegistrationBean(){

        ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        registration.setInitParameters(ImmutableMap.of("loginUsername", "boxfish_pay", "loginPassword", "Fwo0N8GHnfOls8vUOO7SWGaA3Bz2qLP1yclNg="));
        return registration;
    }


//    private HttpMessageConverter fastJsonHttpMessageConverter() {
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//        fastJsonConfig.setFeatures(
//                IgnoreNotMatch,
//                InitStringFieldAsEmpty,
//                AllowSingleQuotes,
//                DisableCircularReferenceDetect
//        );
//        FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
//        fastJsonHttpMessageConverter.setSupportedMediaTypes(Lists.newArrayList(
//                MediaType.APPLICATION_JSON_UTF8,
//                MediaType.TEXT_HTML,
//                MediaType.APPLICATION_XML));
//        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//
//        return fastJsonHttpMessageConverter;
//    }

//    @Override
//    public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
//        for(HttpMessageConverter<?> mc : converters){
//            if(mc instanceof MappingJackson2HttpMessageConverter){
//                ObjectMapper objectMapper = new ObjectMapper();
//                objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//                ((MappingJackson2HttpMessageConverter) mc).setObjectMapper(objectMapper);
//            }
//        }
//    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setStatusCode(HttpStatus.OK).setViewName("index");
    }
}